<?php

namespace app\index\controller;

use app\common\model\Contacts;
use app\common\model\News;
use think\File;
use think\Image;
use think\Controller;
use think\Request;



class ContactsController extends BaseController{

    public function index(){
        $user = cookie('userData');
        $uid = $user['uid'];

        $contactsModel = new Contacts();
        $contactsList = $contactsModel->getAllUserContacts($uid);
        $userData = cookie('userData');
        return view('contacts', ['contactsList' => $contactsList,'userData'=>$userData]);
    }

    public function add(){
        
        if (Request::instance()->isPost()) {
            $name = trim(input('post.aname'));
            $tel = trim(input('post.atel'));
            $email = trim(input('post.aemail'));
            $addr = trim(input('post.aaddr'));
            $msg = trim(input('post.amsg'));

            if($name == '' || $tel == '' ){
                return json(array('code'=>1,'msg'=>'姓名和电话不能为空'));
            }

            $search = '/^0?1[3|4|5|6|7|8][0-9]\d{8}$/';
            if ( !preg_match( $search, $tel ) ) {
                return json(array('code'=>1,'msg'=>'请输入正确的电话号码！'));
            }
            $checkmail="/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/";
            if ( !preg_match( $checkmail, $email ) ) {
                return json(array('code'=>1,'msg'=>'请输入正确的电子邮箱！'));
            }
            
            $user = cookie('userData');
            $uid = $user['uid'];
            $contactsModel = new Contacts();
            
            $contactsModel->add($uid, $name, $tel, $msg, $email, $addr);
            return json(array('code'=>0,'msg'=>'添加成功'));
        }else{
            $user = cookie('userData');

            return view('contacts', ['userData'=>$user]);
        }
    }


    public function edit(){
        if (Request::instance()->isPost()) {
            $tid =  input('post.tid');
            $name = trim(input('post.ename'));
            $tel = trim(input('post.etel'));
            $email = trim(input('post.eemail'));
            $addr = trim(input('post.eaddr'));
            $msg = trim(input('post.emsg'));

            if($name == '' || $tel == '' ){
                return json(array('code'=>1,'msg'=>'姓名和电话不能为空'));
            }

            $contactsModel = new Contacts();
            $contactsData = $contactsModel->getContactsById($tid);
            

            if(count($contactsData)<1){
                return json(array('code'=>1,'msg'=>'没有该联系人'));
            }else{
                $contactsModel->edit($tid, $name, $tel, $msg, $email, $addr);
                return json(array('code'=>0,'msg'=>'修改成功'));
            }
        }else{
            $tid = input('get.tid');
            if(!$tid){
                $this->error('链接错误,返回首页','index/Contacts/');
            }

            $contactsModel = new Contacts();
            $contactsData = $contactsModel->getContactsById($tid)[0];

            $user = cookie('userData');

            return view('edit', ['contactsData'=>$contactsData,'userData'=>$user]);
        }
    }

    public function delete(){
        if (Request::instance()->isPost()) {
            $tid = $username = input('post.tid');

            $contactsModel = new Contacts();
            $contactsData = $contactsModel->getContactsById($tid);

            if(count($contactsData)<1){
                return json(array('code'=>1,'msg'=>'没有该联系人'));
            }else{
                $contactsModel->deleteContacts($tid);
                return json(array('code'=>0,'msg'=>'删除成功'));
            }
        }
    }

}
